/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cardarus.operatingsystem.struts.actions;

import com.cardarus.operatingsystem.domain.SalesTransaction;
import com.cardarus.operatingsystem.model.BusinessModel;
import com.cardarus.operatingsystem.struts.forms.RegisterRewardInputForm;
import com.cardarus.operatingsystem.struts.forms.SalesReportActionForm;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

/**
 *
 * @author nobal
 */
public class SalesReportAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        SalesReportActionForm formBean = (SalesReportActionForm) form;
        Date startDate = formBean.getStartDate();
        Date endDate = formBean.getEndDate();
        Calendar c = Calendar.getInstance();


        if (startDate == null && endDate == null) {
            endDate = new Date();
            c.setTime(endDate);
            c.add(Calendar.DAY_OF_MONTH, -7);
            startDate = c.getTime();
        }
        BusinessModel model = BusinessModel.getInstance();
        List<SalesTransaction> transactions = model.getTransactionsByDate(startDate, endDate);
       double total_SubTotal = 0;
        double total_Tax = 0;
        double total_Total = 0;
        for (SalesTransaction st : transactions) {
            total_SubTotal += st.getSubTotal();
            total_Tax += st.getTax();
            total_Total += st.getTotal();
        }
        HttpSession session = request.getSession();
        session.setAttribute("transactions", transactions);
        session.setAttribute("total_SubTotal", total_SubTotal);
        session.setAttribute("total_Tax", total_Tax);
        session.setAttribute("total_Total", total_Total);
 
        return mapping.findForward(SUCCESS);
    }
}
